package com.nursinghome.admin.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.nursinghome.admin.service.HolidayService;
import com.nursinghome.bean.Holidays;
import com.nursinghome.bean.RespBean;
import com.nursinghome.exception.NurisingHomeException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@CrossOrigin
@RestController
@RequestMapping("/admin/holidays")
public class HolidaysController {
    @Autowired
    private HolidayService holidayService;
    //查询所有的请假通过审核的
    @GetMapping("/allpass")
    public RespBean selectByMname(Integer pageNum ,String mname){
        PageHelper.startPage(pageNum, 5);
        List<Holidays> holidays = holidayService.selectByMname(mname);
        PageInfo<Holidays> pageInfo = new PageInfo<>(holidays);
        return RespBean.ok("",pageInfo);
    }
    //查询所有的请假正在审核的
    @GetMapping("/examineall")
    public RespBean selectAll(Integer pageNum, String mname){
        PageHelper.startPage(pageNum, 3);
        List<Holidays> holidays = holidayService.selectAll(mname);
        PageInfo<Holidays> pageInfo = new PageInfo<>(holidays);
        return  RespBean.ok("",pageInfo);
    }
    //给请假进行审核
    @PutMapping("/{hid}/{state}")
    public RespBean updateState(@PathVariable("hid") Integer hid, @PathVariable("state") Integer state ) throws NurisingHomeException {
        if (holidayService.updateState(state, hid)) {
            return RespBean.ok("审批成功");
        }
        return RespBean.error("审批失败");
    }
    //根据hid查询请假数据
    @GetMapping("/{hid}")
    public RespBean selectByHid(@PathVariable("hid") Integer hid){
        Holidays holidays = holidayService.selectByHid(hid);
        return RespBean.ok("",holidays);
    }
}
